package tankGame05.tank;

import java.util.Vector;

import static tankGame04.tank.Bullet.BULLET_LENGTH;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-03-03
 * Time:15:33
 */
@SuppressWarnings("all")
public class Enemy extends Tank implements Runnable {
    static Vector<Bullet> magazine = new Vector<>();
    Vector<Enemy> enemies = null;
    public Enemy(int x, int y) {
        super(x, y);
    }

    public Enemy(int x, int y, int direct) {
        super(x, y, direct);
    }

    public Enemy(int x, int y, int direct, int speed) {
        super(x, y, direct, speed);
    }

    public void setEnemies(Vector<Enemy> enemies) {
        this.enemies = enemies;
    }
    private boolean isTouchEnemies(){
        Enemy enemy = null;
        for(int i = 0; i < enemies.size(); i++) {
            enemy = enemies.get(i);
            if (this == enemy || !enemy.isState()) continue;
            int tx, ty, ex, ey;
            tx = this.getX();
            ty = this.getY();
            ex = enemy.getX();
            ey = enemy.getY();
            switch(this.getDirect()) {
                case 0:
                    if ((tx >= ex && tx < ex + TANK_SIZE) || (tx + TANK_SIZE >= ex && tx < ex) && (ty >= ey && ty <= ey + TANK_SIZE))
                        return true;
                    break;
                case 1:
                    if (((ty >= ey && ty < ey + TANK_SIZE) || (ty + TANK_SIZE >= ey && ty < ey + TANK_SIZE))
                            && (tx + TANK_SIZE >= ex && tx <= ex))
                        return true;
                    break;
                case 2:
                    if (((tx >= ex && tx < ex + TANK_SIZE) || (tx + TANK_SIZE >= ex && tx < ex))
                            && (ty <= ey && ty + TANK_SIZE >= ey))
                        return true;
                    break;
                case 3:
                    if (((ty >= ey && ty < ey + TANK_SIZE) || (ty + TANK_SIZE >= ey && ty < ey + TANK_SIZE))
                            && (tx <= ex + TANK_SIZE && tx >= ex))
                        return true;
                    break;
            }
        }
        return false;
    }
    @Override
    public void move(int direct, int speed) {
        for (int i = 0; i < 6; i++) {
            switch (direct) {
                case 0:
                    if (!isTouchEnemies() && getY() > 0) setY(getY() - speed);
                    break;
                case 1:
                    if (getX() + TANK_SIZE < 988 && !isTouchEnemies()) setX(getX() + speed);
                    break;
                case 2:
                    if (getY() + TANK_SIZE < 780 && !isTouchEnemies()) setY(getY() + speed);
                    break;
                case 3:
                    if (getX() > 0 && !isTouchEnemies()) setX(getX() - speed);
                    break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        setDirect((int) (Math.random() * 4));
    }

    public void shot() {
        int bx = 0, by = 0;
        by = switch (getDirect()) {
            case 0 -> {
                bx = getX() + TANK_SIZE / 2 - 1;
                yield getY() - BULLET_LENGTH;
            }
            case 1 -> {
                bx = getX() + TANK_SIZE;
                yield getY() + TANK_SIZE / 2 - 1;
            }
            case 2 -> {
                bx = getX() + TANK_SIZE / 2 - 1;
                yield getY() + TANK_SIZE;
            }
            case 3 -> {
                bx = getX() - BULLET_LENGTH;
                yield getY() + TANK_SIZE / 2 - 1;
            }
            default -> by;
        };
        Bullet bullet = new Bullet(bx, by, getDirect(), true);
        new Thread(bullet).start();
        magazine.add(bullet);
    }

    public static Vector<Bullet> getMagazine() {
        return magazine;
    }

    public static void setMagazine(Vector<Bullet> magazine) {
        Enemy.magazine = magazine;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            move(getDirect(), getSpeed());
            if (!isState()) {
                break;
            }
            shot();
        }
    }
}
